<template>
  <div class="login_Container">
    <div class="login_box">
      <div class="avatar_box">
        <img src="../assets/logo.png" alt="" />
      </div>
      <!-- 登录表单区域 -->
      <el-form
        label-width="0px"
        :model="loginForm"
        :rules="rules"
        ref="loginformRef"
        class="login_form"
      >
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input
            prefix-icon="el-icon-user-solid"
            v-model="loginForm.username"
          ></el-input>
        </el-form-item>

        <!-- 用户密码 -->
        <el-form-item prop="password">
          <!--prop 用于表单验证 -->
          <el-input
            prefix-icon="el-icon-unlock"
            v-model="loginForm.password"
            type="password"
          ></el-input>
        </el-form-item>

        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resetLoginForm">重置</el-button>
        </el-form-item>
        
      </el-form>
    </div>
  </div>
</template>
<script>
// http://www.ysqorz.top:8888/api/private/v1/
//https://shop.iwangwei.top/api/v1/
//https://www.liulongbin.top:8888/api/private/v1/login
//http://timemeetyou.com:8889/api/private/v1/
//http://106.53.73.30:8888/api/private/v1/
//http://106.12.11.162:8888/api/private/v1/login
//http://118.31.171.210:8801/api/private/v1/
//http://47.108.206.212:9999/api/private/v1/login
//http://rambuild.cn:8888/api/private/v1
//24
export default {
  data() {
    return {
      //登录表单数据绑定对象
      loginForm: {
        username: 'admin',
        password: '123456',
      },
      //表单验证
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          {
            min: 3,
            max: 10,
            message: '用户名长度在 3 到 10个字符',
            trigger: 'blur',
          },
        ],
          password: [
          { required: true, message: '请输入用户密码', trigger: 'blur' },
          {
            min: 4,
            max: 15,
            message: '用户密码长度在4 到 15个字符',
            trigger: 'blur',
          },
        ],
      },
    }
  },
  methods: {
    //重置按钮
    resetLoginForm() {
      //  console.log(this);
      this.$refs.loginformRef.resetFields() //让值重新定义为loginForm里面的属性
    },
    //登录验证
    login() {
      this.$refs.loginformRef.validate(async (flag) => {
        if (!flag) return
        const { data: res } = await this.$http.post('login', this.loginForm)

        if (res.meta.status !== 200) {
          return this.$message.error('登录失败')
        }
        this.$message.success('登录成功')
        //1.将登录成功之后的token，保存到客户端的sessionStorage中
        //  1.1项目中除了登录之外的其他API接口，必须在登录之后才能访问
        //  1.2token 只应在当前网站打开期间生效，所以将token保存在sessionStorage中
        console.log(res)
        window.sessionStorage.setItem('token', res.data.token)
        //2.通过编程式导航跳转到后台主页，路由地址是 /home
        this.$router.push('/home')
      })
    },
  },
}
</script>

<style  scoped>
.login_Container {
  background-color: #2b4b6b;
  color: white;
  height: 100%;
}
.login_box {
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 3px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
.avatar_box {
  height: 130px;
  width: 130px;
  border: 1px solid #eee;
  border-radius: 50%;
  padding: 10px;
  box-shadow: 0 0 10px #ddd;
  position: absolute;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: #fff;
}
img {
  width: 100%;
  height: 100%;
  border-radius: 50%;
  background-color: #eee;
}

/* 表单 */
.login_form .btns {
  display: flex;
  justify-content: flex-end;
}
.login_form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}
</style>
